package com.oms.base;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;

/**
 * 类说明：该类用于加载SiS平台运行时配置信息的持有者，是一个助手类用于方便的得到配置文件中每一个属性的值
 */
public class OMSConfigHolder {
	
	protected final Log logger = LogFactory.getLog(OMSConfigHolder.class);
	private static OMSConfigHolder config ;
	private Resource location;
	 
	/**
	 * 获取运行时配置信息，对应的是/WEB-INF/config/oms-config.properties文件
	 * @return 返回oms-config.properties文件的Properties对象
	 */
	public Properties getConfig() {
		Properties props = null;

		try {
        	props = new Properties();
        	logger.info("loading properties file :" + location.getFilename());
        	InputStream is = location.getInputStream();
			props.load( is );
			is.close();
		} catch (IOException e) {
			logger.error("Unable to load " +  location.getFilename(), e);
			return null;
			
		}
		return props;
	}

	public Resource getLocation() {
		return location;
	}

	public void setLocation(Resource location) {
		this.location = location;
	}
	
	public static Properties getProperties() {
		if (config == null) {
			config = (OMSConfigHolder) SpringContextFactory.getBean("omsConfig");
		}
		return config.getConfig();
	}
	
	public static String getProperty(String key) {
		return getProperties().getProperty(key);
	}
	
	/**
	 * 获取web根路径
	 * @return
	 */
	public static String getWebRoot() {
		return System.getProperty(getProperties().getProperty("web.root"));
	}
}